<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Search extends Controller_Initial_Page {
    
    const project_limit = 24;
    const people_limit = 24; 

    public function action_index()
    {     
           $arr_get = array('search_word' => '',
                            'company'     => '',
                            'employed'     => '',
                            'not_employed' => '',
                            'field'       => '',
                            'country'     => '',
                            'location'    => '',
                            'school'      => '',
                            'joined'      => '',
                            'browse'      => '',
                            'time'        => '',
                            'type'        => '2');
                         
            $arr_get = $this->parse_get_array($_GET, $arr_get);

            // prepare search inputs
            $field_model = ORM::factory('field');
            $first_filds_option = array( __('All fields'));
            $fields = $field_model->get_all();
            $fields = $first_filds_option + $fields; 

            $content_view = View::factory('content');           
            $search_template_view = View::factory('search/template');

            $country_model = ORM::factory('country');
            $first_country_option = array(0 => __('All countries'));
            $countries = $country_model->get_all();
            $countries = $first_country_option + $countries;
            $search_template_view->set('countries',$countries);
                   
           if($arr_get['type'] === '1')
           {               
               $arr_get['limit'] = self::people_limit;
               $arr_get['offset'] = 0;
               
               $profile_model = ORM::factory('user_profile');
               $profiles = $profile_model->get_profiles($arr_get);
               
               $search_index_view = View::factory('search/content_people');
               $search_index_view->set('profiles',$profiles);
               
               $result = count($profiles);
           }
           elseif($arr_get['type'] === '2')
           {
               $project_model = ORM::factory('desktop_project');
               
               $arr_get['field'] = $arr_get['field'] !== '0' ? $arr_get['field'] : null;
               $arr_get['time'] =  $arr_get['time'] !== '0' ? $arr_get['time'] : null;
               $arr_get['browse'] = (int) $arr_get['browse'];
               
               $projects = $project_model->get_projects(self::project_limit,
                                                        null,
                                                        $arr_get['field'], 
                                                        $arr_get['search_word'],  
                                                        $arr_get['time'], 
                                                        $arr_get['browse']);
                    
               $search_index_view = View::factory('search/content_projects');
               $search_index_view->set('projects', $projects);
               
               $result = count($projects);
           }
           else
           {
               throw new HTTP_Exception_404;
           }           
           
           $search_template_view->set('get_arr',$arr_get);
           $search_template_view->set('fields',$fields);
           $search_template_view->set('all_result', $result);
                   
           $search_template_view->set('content',$search_index_view);                   
           $content_view->set('content',$search_template_view);                
           $this->template->set('body',$content_view);           
    }
    
    public function action_ajax_index()
    {
           $arr_post = array('search_word' => '',
                            'company'     => '',
                            'employed'    => '',
                            'not_employed'=> '',
                            'field'       => '',
                            'country'     => '',
                            'location'    => '',
                            'school'      => '',
                            'joined'      => '',
                            'browse'      => '',
                            'time'        => '',
                            'type'        => '',
                            'page'        => '1');
                         
           $arr_post = $this->parse_get_array($_POST, $arr_post);
            
           if($arr_post['type'] === '1')
           {               
               $arr_post['limit'] = self::people_limit;
               $arr_post['offset'] = ($arr_post['page']-1) * self::people_limit;
               
               $profile_model = ORM::factory('user_profile');
               $profiles = $profile_model->get_profiles($arr_post);
               
               $search_index_view = View::factory('search/content_people');
               $search_index_view->set('profiles',$profiles);
               $search_index_view->set('page',(int) $arr_post['page']);
               
           }
           elseif($arr_post['type'] === '2')
           {
               $project_model = ORM::factory('desktop_project');
               
               $arr_post['field'] = $arr_post['field'] !== '0' ? $arr_post['field'] : null;
               $arr_post['time'] =  $arr_post['time'] !== '0' ? $arr_post['time'] : null;
               $arr_post['browse'] = (int) $arr_post['browse'];
               $offset = ($arr_post['page']-1) * self::project_limit;
               
               $projects = $project_model->get_projects(self::project_limit,
                                                        $offset,
                                                        $arr_post['field'], 
                                                        $arr_post['search_word'],  
                                                        $arr_post['time'], 
                                                        $arr_post['browse']);
                    
               $search_index_view = View::factory('search/content_projects');
               $search_index_view->set('projects', $projects);
               $search_index_view->set('page',(int) $arr_post['page']);
               
             
           }
           else
           {
               throw new HTTP_Exception_404;
           }
           
           $output= array('success' => true,
                          'content' => $search_index_view->render());
                               
           $this->response->body(json_encode($output));
    }
    
    private function parse_get_array($get_array, $expected_arr_get)
    {
         
           
           foreach($get_array as $key => $value )
           {
               
               if(array_key_exists($key, $expected_arr_get))
               {
                   $expected_arr_get[$key] = Security::xss_clean($value);
               }
               
           }
           
           return  $expected_arr_get;
    }
    
}